package com.crazyauntzhang.easyshopbackend.exception;

import com.crazyauntzhang.easyshopbackend.result.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理：处理登录拦截器抛出的异常
 */
@ControllerAdvice
public class LoginControllerAdvice {

	@ResponseBody
	@ExceptionHandler(RuntimeException.class)
	public ResponseResult<Object> loginControllerAdvice(RuntimeException e) {
		//获取异常信息，如果与约定的异常信息相等则认为是登录异常，返回响应体，否则直接返回异常信息
		String message = e.getMessage();
		if (message.equals("Invalid Token"))
			return new ResponseResult<>(10401, "Invalid Token");
		return new ResponseResult<>(message);
	}
}
